﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyTimer_cool
{
    public partial class Form1 : Form
    {
        private int _nowSecond = 0;

        private Boolean _colonFlag = true;

        private Bitmap[] _bmpShow = new Bitmap[13];
        
        public Form1()
        {
            InitializeComponent();

            _nowSecond = 0;

            _bmpShow[0] = new Bitmap("images/0.bmp");
            _bmpShow[1] = new Bitmap("images/1.bmp");
            _bmpShow[2] = new Bitmap("images/2.bmp");
            _bmpShow[3] = new Bitmap("images/3.bmp");
            _bmpShow[4] = new Bitmap("images/4.bmp");
            _bmpShow[5] = new Bitmap("images/5.bmp");
            _bmpShow[6] = new Bitmap("images/6.bmp");
            _bmpShow[7] = new Bitmap("images/7.bmp");
            _bmpShow[8] = new Bitmap("images/8.bmp");
            _bmpShow[9] = new Bitmap("images/9.bmp");
            _bmpShow[10] = new Bitmap("images/dot1.bmp");
            _bmpShow[11] = new Bitmap("images/dot2.bmp");
            _bmpShow[12] = new Bitmap("images/blank.bmp");
        }

        private Image GetResourceImage(string displayStr)
        {
            if (displayStr == "0")
                return _bmpShow[0];
            else if (displayStr == "1")
                return _bmpShow[1];
            else if (displayStr == "1")
                return _bmpShow[1];
            else if (displayStr == "2")
                return _bmpShow[2];
            else if (displayStr == "3")
                return _bmpShow[3];
            else if (displayStr == "4")
                return _bmpShow[4];
            else if (displayStr == "5")
                return _bmpShow[5];
            else if (displayStr == "6")
                return _bmpShow[6];
            else if (displayStr == "7")
                return _bmpShow[7];
            else if (displayStr == "8")
                return _bmpShow[8];
            else if (displayStr == "9")
                return _bmpShow[9];
            else if (displayStr == ".")
                return _bmpShow[10];
            else if (displayStr == ":")
                return _bmpShow[11];
            else
                return _bmpShow[12];
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            pictureBoxHour1.Image = GetResourceImage("0");
            pictureBoxHour2.Image = GetResourceImage("0");
            pictureBoxMintue1.Image = GetResourceImage("0");
            pictureBoxMinute2.Image = GetResourceImage("0");
            pictureBoxSecond1.Image = GetResourceImage("0");
            pictureBoxSecond2.Image = GetResourceImage("0");
            pictureBoxColon1.Image = GetResourceImage(":");
            pictureBoxColon2.Image = GetResourceImage(":");
            _nowSecond = 0;
            timer1.Enabled = true;
            timer2.Enabled = true;
            buttonStart.Enabled = false;
            buttonPause.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBoxHour1.Image = GetResourceImage("0");
            pictureBoxHour2.Image = GetResourceImage("0");
            pictureBoxMintue1.Image = GetResourceImage("0");
            pictureBoxMinute2.Image = GetResourceImage("0");
            pictureBoxSecond1.Image = GetResourceImage("0");
            pictureBoxSecond2.Image = GetResourceImage("0");
            pictureBoxColon1.Image = GetResourceImage(":");
            pictureBoxColon2.Image = GetResourceImage(":");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _nowSecond++;

            int hour = _nowSecond / 3600;
            int minute = (_nowSecond % 3600) / 60;
            int second = (_nowSecond % 3600) % 60;

            this.pictureBoxSecond1.Image = GetResourceImage((second / 10).ToString());
            this.pictureBoxSecond2.Image = GetResourceImage((second % 10).ToString());

            this.pictureBoxMintue1.Image = GetResourceImage((minute / 10).ToString());
            this.pictureBoxMinute2.Image = GetResourceImage((minute % 10).ToString());

            this.pictureBoxHour1.Image = GetResourceImage((hour / 10).ToString());
            this.pictureBoxHour2.Image = GetResourceImage((hour % 10).ToString());
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            _colonFlag = !_colonFlag;
            if (_colonFlag == true)
            {
                pictureBoxColon1.Image = GetResourceImage(" ");
                pictureBoxColon2.Image = GetResourceImage(" ");
            }
            else
            {
                pictureBoxColon1.Image = GetResourceImage(":");
                pictureBoxColon2.Image = GetResourceImage(":");
            }
        }

        private void buttonPause_Click(object sender, EventArgs e)
        {
            if (buttonPause.Text == "暂停")
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                buttonStart.Enabled = true;
                buttonPause.Text = "继续";
            }
            else
            {
                timer1.Enabled = true;
                timer2.Enabled = true;
                buttonStart.Enabled = false;
                buttonPause.Text = "暂停";
            }
        }

        private void pictureBoxMintue1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Move(object sender, EventArgs e)
        {
            int screenRight = Screen.PrimaryScreen.Bounds.Right;
            int formRight = this.Left + this.Size.Width;
            if (Math.Abs(screenRight - formRight) <= 100)
                this.Left = screenRight - this.Size.Width;
            if (Math.Abs(this.Left) <= 100)
                this.Left = 0;

            int screenBottom = Screen.PrimaryScreen.Bounds.Bottom;
            int formBottom = this.Top + this.Size.Height;
            if (Math.Abs(screenBottom - formBottom) <= 50)
                this.Top = screenBottom - this.Size.Height;
            if (Math.Abs(this.Top) <= 50)
                this.Top = 0;
        }
    }
}
